package sixth.group.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sixth.group.domain.Administrator;
import sixth.group.domain.Customer;
import sixth.group.domain.Orderinf;
import sixth.group.domain.Restaurant;
import sixth.group.service.XyxAdministratorService;
import sixth.group.utils.MessageAndData;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/xyxAdministrator")
public class XyxAdministratorController {

    @Autowired
    private XyxAdministratorService xyxAdministratorService;

    @RequestMapping(value = "/login")
    public ModelAndView administratorLogin(Administrator administrator, HttpSession httpSession) {
        ModelAndView modelAndView = new ModelAndView();
        boolean status = xyxAdministratorService.administratorLoginCheck(administrator);
        if (status) {
            httpSession.setAttribute("Adm_SESSION", administrator);
            modelAndView.setViewName("redirect:../XyxAdministratorMainPage.html");
        } else {
            modelAndView.setViewName("redirect:../XyxAdministratorLoginPage.html");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/AdministratorPassChange")
    public ModelAndView administratorPassChange(Administrator administrator) {
        ModelAndView modelAndView = new ModelAndView();
        boolean status = xyxAdministratorService.changeAdministratorPass(administrator);
        if (status) {
            modelAndView.setViewName("redirect:../XyxAdministratorLoginPage.html");
        } else {
            modelAndView.setViewName("redirect:../XyxAdministratorLoginPage.html");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/orderInfList")
    @ResponseBody
    public MessageAndData orderInfList() {
        List<Orderinf> orderinfs = xyxAdministratorService.scanOrderInf();
        MessageAndData orderInfList = MessageAndData.success().add("orderInfList", orderinfs);
        return orderInfList;
    }

    @RequestMapping(value = "/orderInfDelete")
    @ResponseBody
    public ModelAndView orderInfDelete(Orderinf orderinf) {
        ModelAndView modelAndView = new ModelAndView();
        boolean status = xyxAdministratorService.deleteOrder(orderinf);
        if (status) {
            modelAndView.setViewName("redirect:../XyxAdminiOrderManage.html");
        } else {
            modelAndView.setViewName("redirect:../XyxAdminiOrderManage.html");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/ResPerChange")
    public ModelAndView ResPerChange(Restaurant restaurant) {
        ModelAndView modelAndView = new ModelAndView();
        restaurant.setRpermission(restaurant.getRpermission()^1);
        boolean status = xyxAdministratorService.changeRestaurantPermission(restaurant);
        if (status) {
            modelAndView.setViewName("redirect:../WyxResLook.html");
        } else {
            modelAndView.setViewName("redirect:../WyxResLook.html");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/CusPerChange")
    public ModelAndView CusPerChange(Customer customer) {
        ModelAndView modelAndView = new ModelAndView();
        customer.setCpermission(customer.getCpermission()^1);
        boolean status = xyxAdministratorService.changeCustomerPermission(customer);
        if (status) {
            modelAndView.setViewName("redirect:../WyxCusLook.html");
        } else {
            modelAndView.setViewName("redirect:../WyxCusLook.html");
        }
        return modelAndView;
    }
}
